package com.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;


@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {

    private JdbcProperties jdbcProperties;

    public JdbcConfig(JdbcProperties properties) {
        jdbcProperties = properties;
    }

    @Bean
    public DataSource dataSource(JdbcProperties pro) {
        DruidDataSource dataSource = new DruidDataSource();
//        try {
//            System.out.println("数据源： "+dataSource.getClass());
//            Connection connection = dataSource.getConnection();
//            System.out.println("数据源连接： "+connection);
//            connection.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }

        dataSource.setDriverClassName(pro.getDriverClassName());
        dataSource.setUrl(pro.getUrl());
        dataSource.setUsername(pro.getUsername());
        dataSource.setPassword(pro.getPassword());
        System.out.println("Hello Debug:" + dataSource.getPassword());
        return dataSource;
    }

}
